import com.gxa.mapper.EmployeeMapper;
import com.gxa.pojo.Employee;
import com.sun.org.apache.xpath.internal.SourceTree;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: 赵贺益
 * Date: 2021/11/17 12:54
 * Description:
 * Version: V1.0
 */
public class Mybatis {
    SqlSession sqlSession;
    @Before//执行所有的Test方法之前访问中国方法
    public  void  before() throws IOException {
//        读取的mybatis核心配置文件
        String resource = "Mybatis.xml";
//        通过流的方式读取核心配置文件，把它加载到内存中
        InputStream inputStream = Resources.getResourceAsStream(resource);
//        创建SqlSessionFactory工厂
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//        通过工厂对象调用获得SqlSession对象  //默认是手动提交；如果要改变为自动提交，则设置为true表示自动提交
        sqlSession=sqlSessionFactory.openSession(true);
//
    }
    @Test
    public  void testMybatis() throws IOException {
//
//      获得employeeMapper对象
        EmployeeMapper employeeMapper=sqlSession.getMapper(EmployeeMapper.class);

//        向employee表插入一条数据
//        int result=employeeMapper.addEmployee("888贺益","2255151515");
//        System.out.println(result);

//        向employee表更新一条数据
//           int result=employeeMapper.updateEmployee("武大郎","123457",123457);
//            System.out.println(result);

//        向employee查询出一个实体
//        EmployeeMapper employeeMapper=sqlSession.getMapper(EmployeeMapper.class);
//          Employee employee=employeeMapper.queryEmployee(123457);
//          System.out.println(employee);

//          删除一条员工信息
//        int result=employeeMapper.deleteEmployee(123460);
//        System.out.println(result);

//        查询获得一个集合对象
//        List<Employee> employeeList =employeeMapper.queryEmployeeList();
//        System.out.println(employeeList);

//        传递一个employee对象进行数据库插入操作
//        Employee employee=new Employee();
//        employee.setE_name("谢志浩");
//        employee.setE_password("5201314");
//        int result=employeeMapper.insertEmployee(employee);
//        System.out.println(result);

//        传入一个对象，修改数据库中的信息
//        Employee employee=new Employee();
//        employee.setE_password("0000000000000000000000000000");
//        int result=employeeMapper.updateEmployees(employee);
//        System.out.println(result);

//        传入一个map对象，向数据库中插入信息
        Map<String,String> map=new HashMap<String,String>();
        map.put("e_name","谢志浩");
        map.put("e_password","12345678");
        int result =employeeMapper.insertEmployees(map);
        System.out.println(result);


    }
}
